package fr.expdev.bndd;

import java.lang.reflect.Method;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import fr.expdev.bndd.action.Action;
import fr.expdev.bndd.data.Configuration;
import fr.expdev.bndd.data.Configuration.Key;
import fr.expdev.bndd.widget.CheckItem;
import greendroid.app.GDListActivity;
import greendroid.widget.ItemAdapter;
import greendroid.widget.item.SeparatorItem;
import greendroid.widget.item.TextItem;

public class ConfigurationActivity extends GDListActivity {

	/**
	 * Adapter pour la liste de items
	 */
	protected final ItemAdapter adapter = new ItemAdapter(this);

	/**
	 * Widget ListView qui presente les posts
	 */
	protected ListView listView;

	/**
	 * Item : AutoRefresh
	 */
	protected CheckItem autoRefreshItem;
	
	/**
	 * Item : Every X Minutes
	 */
	protected TextItem everyXMitnuesItem;

	/**
	 * Item : StartupRefresh
	 */
	protected CheckItem startupRefreshItem;
	

	/**
	 * Action sur l'item AutoRefresh
	 */
	protected Action autoRefreshAction = new Action() {
		@Override
		public void execute(Object... objects) {
			Configuration config = Configuration .getInstance(ConfigurationActivity.this);
			config.setAutoRefreshEnabled((Boolean)objects[0]);
			
			if ((Boolean) objects[0]) {
				adapter.insert(everyXMitnuesItem, 2);
			} else {
				adapter.remove(everyXMitnuesItem);
			}
			adapter.notifyDataSetChanged();
		}
	};
	/*
	protected Action autoRefreshAction = new Action() {

		@Override
		public void execute(Object... objects) {
			final Configuration config = Configuration.getInstance(ConfigurationActivity.this);
			final Boolean newValue = (Boolean)objects[0];

			if (newValue) {
				// On fais choisir l'interval
				View numberPickerView = getLayoutInflater().inflate(R.layout.number_picker, null);
				final Object o = numberPickerView.findViewById(R.id.number_picker);
				try {
					Method m = o.getClass().getMethod("setRange", int.class, int.class);
					m.invoke(o, 0, 9999);
					m = o.getClass().getMethod("setCurrent", int.class);
					m.invoke(o, Configuration.getInstance(ConfigurationActivity.this).getInt(Key.AUTOREFRESH_INTERVAL));
				} catch (Exception e) {
					Log.e("Probleme avec le NumberPicker", e.getMessage());
				}

				AlertDialog.Builder builder = new AlertDialog.Builder(ConfigurationActivity.this);
				builder.setMessage(getString(R.string.interval_in_minutes))
					.setCancelable(true)
					.setView(numberPickerView)
					.setPositiveButton(getString(R.string.validate), new OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							config.setBoolean(Key.AUTOREFRESH_ENABLED, newValue);
							try {
								// On récupère la valeur
								Method m = o.getClass().getMethod( "getCurrent");
								Integer value = (Integer) m.invoke(o);

								// On enregistre la config
								config.setInt(Key.AUTOREFRESH_INTERVAL, value);

								// On met à jour l'item
								ConfigurationActivity.this.autoRefreshItem.setSubtitle(getResources().getQuantityString(R.plurals.every_x_minutes, value, value));
								ConfigurationActivity.this.adapter.notifyDataSetChanged();
							} catch (Exception e) {
								Log.e("Probleme avec le NumberPicker", e.getMessage());
							}
						}
					})
					.setOnCancelListener(new OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface dialog) {
							autoRefreshItem.setChecked(false);
							ConfigurationActivity.this.adapter.notifyDataSetChanged();
						}
					});

				Dialog dialog = builder.create();
				dialog.show();

			} else {
				config.setBoolean(Key.AUTOREFRESH_ENABLED, newValue);
			}
		}
	};*/

	/**
	 * Action sur l'item StartupRefresh
	 */
	protected Action startupRefreshAction = new Action() {
		@Override
		public void execute(Object... objects) {
			Configuration config = Configuration .getInstance(ConfigurationActivity.this);
			config.setRefreshAtStartupEnabled((Boolean)objects[0]);
		}
	};

	public ConfigurationActivity() {}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(getString(R.string.configuration));

		Configuration config = Configuration.getInstance(this);

		int minutes = config.getAutoRefreshInterval();
		
		autoRefreshItem = new CheckItem(getString(R.string.automatically),
				config.isAutoRefreshEnabled(), autoRefreshAction);
		
		everyXMitnuesItem = new TextItem(getResources().getQuantityString(R.plurals.every_x_minutes, minutes, minutes));
		
		startupRefreshItem = new CheckItem(getString(R.string.at_startup),
				config.isRefreshAtStartupEnabled(), startupRefreshAction);

		adapter.add(new SeparatorItem(getString(R.string.refresh)));
		adapter.add(autoRefreshItem);
		if (config.isAutoRefreshEnabled()) {
			adapter.add(everyXMitnuesItem);
		}
		adapter.add(startupRefreshItem);
		adapter.add(new SeparatorItem(getString(R.string.reinit)));
		adapter.add(new TextItem(getString(R.string.reinit_config)));

		setListAdapter(adapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		switch(position) {
			case 4:
				AlertDialog.Builder builder = new AlertDialog.Builder(ConfigurationActivity.this);
				builder.setMessage(getString(R.string.do_you_want_to_reset_everything))
					.setCancelable(true)
					.setPositiveButton(getString(R.string.validate), new OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							
							Configuration config = Configuration.getInstance(ConfigurationActivity.this);
							int minutes = config.getAutoRefreshInterval();
							
							config.reset();
							autoRefreshItem.setChecked(config.isAutoRefreshEnabled());
							autoRefreshItem.subtitle = getResources().getQuantityString(R.plurals.every_x_minutes, minutes, minutes);
							
							startupRefreshItem.setChecked(config.isRefreshAtStartupEnabled());
							
							adapter.notifyDataSetChanged();
							
						}
					}).setNegativeButton(getString(R.string.cancel), null);
				
				builder.create().show();
				break;
		}
		super.onListItemClick(l, v, position, id);
	}

}
